package com.ruoyi.zhgd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.zhgd.domain.bo.NAsphaltDataRatioBo;
import com.ruoyi.zhgd.domain.vo.NAsphaltDataRatioVo;
import com.ruoyi.zhgd.domain.NAsphaltDataRatio;
import com.ruoyi.zhgd.mapper.NAsphaltDataRatioMapper;
import com.ruoyi.zhgd.service.INAsphaltDataRatioService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 沥青拌合站数据Service业务层处理
 *
 * @author zyb
 * @date 2023-09-06
 */
@RequiredArgsConstructor
@Service
@DS("sjcj")
public class NAsphaltDataRatioServiceImpl implements INAsphaltDataRatioService {

    private final NAsphaltDataRatioMapper baseMapper;

    /**
     * 查询沥青拌合站数据
     */
    @Override
    public NAsphaltDataRatioVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询沥青拌合站数据列表
     */
    @Override
    public TableDataInfo<NAsphaltDataRatioVo> queryPageList(NAsphaltDataRatioBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<NAsphaltDataRatio> lqw = buildQueryWrapper(bo);
        Page<NAsphaltDataRatioVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询沥青拌合站数据列表
     */
    @Override
    public List<NAsphaltDataRatioVo> queryList(NAsphaltDataRatioBo bo) {
        LambdaQueryWrapper<NAsphaltDataRatio> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<NAsphaltDataRatio> buildQueryWrapper(NAsphaltDataRatioBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<NAsphaltDataRatio> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getAsphaltRatioN() != null, NAsphaltDataRatio::getAsphaltRatioN, bo.getAsphaltRatioN());
        lqw.eq(bo.getGravel1RatioN() != null, NAsphaltDataRatio::getGravel1RatioN, bo.getGravel1RatioN());
        lqw.eq(bo.getGravel2RatioN() != null, NAsphaltDataRatio::getGravel2RatioN, bo.getGravel2RatioN());
        lqw.eq(bo.getGravel3RatioN() != null, NAsphaltDataRatio::getGravel3RatioN, bo.getGravel3RatioN());
        lqw.eq(bo.getGravel4RatioN() != null, NAsphaltDataRatio::getGravel4RatioN, bo.getGravel4RatioN());
        lqw.eq(bo.getGravel5RatioN() != null, NAsphaltDataRatio::getGravel5RatioN, bo.getGravel5RatioN());
        lqw.eq(bo.getGravel6RatioN() != null, NAsphaltDataRatio::getGravel6RatioN, bo.getGravel6RatioN());
        lqw.eq(bo.getPowder1RatioN() != null, NAsphaltDataRatio::getPowder1RatioN, bo.getPowder1RatioN());
        lqw.eq(bo.getPowder2RatioN() != null, NAsphaltDataRatio::getPowder2RatioN, bo.getPowder2RatioN());
        lqw.eq(bo.getPowder3RatioN() != null, NAsphaltDataRatio::getPowder3RatioN, bo.getPowder3RatioN());
        lqw.eq(bo.getRecycledMaterialRatioN() != null, NAsphaltDataRatio::getRecycledMaterialRatioN, bo.getRecycledMaterialRatioN());
        lqw.eq(bo.getAsphaltRatioW() != null, NAsphaltDataRatio::getAsphaltRatioW, bo.getAsphaltRatioW());
        lqw.eq(bo.getGravel1RatioW() != null, NAsphaltDataRatio::getGravel1RatioW, bo.getGravel1RatioW());
        lqw.eq(bo.getGravel2RatioW() != null, NAsphaltDataRatio::getGravel2RatioW, bo.getGravel2RatioW());
        lqw.eq(bo.getGravel3RatioW() != null, NAsphaltDataRatio::getGravel3RatioW, bo.getGravel3RatioW());
        lqw.eq(bo.getGravel4RatioW() != null, NAsphaltDataRatio::getGravel4RatioW, bo.getGravel4RatioW());
        lqw.eq(bo.getGravel5RatioW() != null, NAsphaltDataRatio::getGravel5RatioW, bo.getGravel5RatioW());
        lqw.eq(bo.getGravel6RatioW() != null, NAsphaltDataRatio::getGravel6RatioW, bo.getGravel6RatioW());
        lqw.eq(bo.getPowder1RatioW() != null, NAsphaltDataRatio::getPowder1RatioW, bo.getPowder1RatioW());
        lqw.eq(bo.getPowder2RatioW() != null, NAsphaltDataRatio::getPowder2RatioW, bo.getPowder2RatioW());
        lqw.eq(bo.getPowder3RatioW() != null, NAsphaltDataRatio::getPowder3RatioW, bo.getPowder3RatioW());
        lqw.eq(bo.getRecycledMaterialRatioW() != null, NAsphaltDataRatio::getRecycledMaterialRatioW, bo.getRecycledMaterialRatioW());
        return lqw;
    }

    /**
     * 新增沥青拌合站数据
     */
    @Override
    public Boolean insertByBo(NAsphaltDataRatioBo bo) {
        NAsphaltDataRatio add = BeanUtil.toBean(bo, NAsphaltDataRatio.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改沥青拌合站数据
     */
    @Override
    public Boolean updateByBo(NAsphaltDataRatioBo bo) {
        NAsphaltDataRatio update = BeanUtil.toBean(bo, NAsphaltDataRatio.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(NAsphaltDataRatio entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除沥青拌合站数据
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 生产数据监控内掺配比数据分页
     *
     * @param bo
     * @param pageQuery
     * @return
     */
    @Override
    public TableDataInfo<NAsphaltDataRatioVo> inPage(NAsphaltDataRatioBo bo, PageQuery pageQuery) {
        Page<NAsphaltDataRatioVo> result = baseMapper.inPage(pageQuery.build(), bo);
        return TableDataInfo.build(result);
    }

    /**
     * 生产数据监控外掺配比数据分页
     *
     * @param bo
     * @param pageQuery
     * @return
     */
    @Override
    public TableDataInfo<NAsphaltDataRatioVo> outPage(NAsphaltDataRatioBo bo, PageQuery pageQuery) {
        Page<NAsphaltDataRatioVo> result = baseMapper.outPage(pageQuery.build(), bo);
        return TableDataInfo.build(result);
    }
}
